package main

import (
	"flag"
	"fmt"
	"net/http"
	"os"
	"strings"
)

// serve dir

func main() {
	dir := flag.String("dir", "", "serve dir")
	addr := flag.String("addr", ":8080", "serve address")
	flag.Parse()

	if *dir == "" {
		wd, _ := os.Getwd()
		*dir = wd
	}

	http.Handle("/", http.FileServer(http.Dir(*dir)))

	if strings.HasPrefix(*addr, ":") {
		fmt.Printf("start serve %s at: http://localhost%s", *dir, *addr)
	} else {
		fmt.Printf("start serve %s at: %s", *dir, *addr)
	}
	fmt.Println()

	err := http.ListenAndServe(*addr, nil)
	if err != nil {
		fmt.Println(err)
	}
}
